﻿using System;
using System.Text;

namespace Oddo.Concept.Crm.Common.Exceptions
{
    public class RequestException : System.Exception
    {
        public RequestException()
        {
        }

        public RequestException(string message) 
            : base(message)
        {
        }

        public RequestException(string message, Exception innerException) 
            : base(message, innerException)
        {
        }

        public string UnwrapExceptionMessage()
        {
            var sb = new StringBuilder();
            sb.AppendLine(base.Message);
            if(base.InnerException!=null)
                sb.AppendLine(base.InnerException.Message);

            if (base.InnerException is AggregateException && ((AggregateException)InnerException).InnerExceptions !=null)
            {
                foreach (var exp in ((AggregateException) InnerException).InnerExceptions)
                    sb.AppendLine(exp.Message);
            }
            else if(base.InnerException!=null)
            {
                var exp = base.InnerException.InnerException;
                while (exp != null)
                {
                    sb.AppendLine(exp.Message);
                    exp = exp.InnerException;
                }
            }
            return sb.ToString();
        }
    }
}